var exports = {};

/*******************
 ****兼容ie 不能console调试问题***
 *******************/
window.console = window.console || (function() {
	var c = {};
	c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace = c.assert = function() {};
	return c;
})();

/*
 * 数组兼容
 */
if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for(var i = 0; i < this.length; i++) {
			if(this[i] == obj) {
				return i;
			}
		}
		return -1;
	}
};

/*
 * 获取url参数
 */
window.GetURLRequest = function() {
	var url = location.search; //获取url中"?"符后的字串 
	var theRequest = new Object();
	if(url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for(var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}

//十六进制颜色值的正则表达式  
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
/*RGB颜色转换为16进制*/
String.prototype.colorHex = function() {
	var that = this;
	if(/^(rgb|RGB)/.test(that)) {
		var aColor = that.replace(/(?:||rgb|RGB)*/g, "").split(",");
		var strHex = "#";
		for(var i = 0; i < aColor.length; i++) {
			var hex = Number(aColor[i]).toString(16);
			if(hex === "0") {
				hex += hex;
			}
			strHex += hex;
		}
		if(strHex.length !== 7) {
			strHex = that;
		}
		return strHex;
	} else if(reg.test(that)) {
		var aNum = that.replace(/#/, "").split("");
		if(aNum.length === 6) {
			return that;
		} else if(aNum.length === 3) {
			var numHex = "#";
			for(var i = 0; i < aNum.length; i += 1) {
				numHex += (aNum[i] + aNum[i]);
			}
			return numHex;
		}
	} else {
		return that;
	}
};

//-------------------------------------------------  
/*16进制颜色转为RGB格式*/
String.prototype.colorRgb = function() {
	var sColor = this.toLowerCase();
	if(sColor && reg.test(sColor)) {
		if(sColor.length === 4) {
			var sColorNew = "#";
			for(var i = 1; i < 4; i += 1) {
				sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
			}
			sColor = sColorNew;
		}
		//处理六位的颜色值  
		var sColorChange = [];
		for(var i = 1; i < 7; i += 2) {
			sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
		}
		return sColorChange.join(",");
	} else {
		return sColor;
	}
};

Date.prototype.format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, //month 
		"d+": this.getDate(), //day 
		"h+": this.getHours(), //hour 
		"m+": this.getMinutes(), //minute 
		"s+": this.getSeconds(), //second 
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
		"S": this.getMilliseconds() //millisecond 
	}

	if(/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}

	for(var k in o) {
		if(new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
};

Date.prototype.dateFormat = function(formatStr, date) {
	var format = formatStr ? formatStr : 'yyyy-MM-DD HH:mm:ss';
	var time = date ? this.string2Date(date) : new Date();

	var week = ['日', '一', '二', '三', '四', '五', '六'];
	var weekName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

	format = format.replace(/yyyy|YYYY/, time.getFullYear());
	format = format.replace(/yy|YY/g, (time.getYear() % 100) > 9 ? (time.getYear() % 100).toString() : '0' +
		(time.getYear() % 100));

	format = format.replace(/MM/, time.getMonth() + 1 > 9 ? (time.getMonth() + 1).toString() : '0' +
		(time.getMonth() + 1));
	format = format.replace(/M/g, time.getMonth() + 1);

	format = format.replace(/dd|DD/, time.getDate() > 9 ? time.getDate().toString() : '0' + time.getDate());
	format = format.replace(/d|D/g, time.getDate());

	format = format.replace(/HH/, time.getHours() > 9 ? time.getHours().toString() : '0' + time.getHours());
	format = format.replace(/H/g, time.getHours());

	format = format.replace(/hh/, (time.getHours() % 12) > 9 ? (time.getHours() % 12).toString() : '0' +
		(time.getHours() % 12));
	format = format.replace(/h/g, (time.getHours() % 12));

	format = format.replace(/mm/, time.getMinutes() > 9 ? time.getMinutes().toString() : '0' +
		time.getMinutes());
	format = format.replace(/m/g, time.getMinutes());

	format = format.replace(/ss/, time.getSeconds() > 9 ? time.getSeconds().toString() : '0' +
		time.getSeconds());
	format = format.replace(/s/g, time.getSeconds());

	format = format.replace(/SSS/g, time.getMilliseconds() > 99 ? time.getMilliseconds().toString() : time
		.getMilliseconds() > 9 ? '0' + time.getMilliseconds() : '00' + time.getMilliseconds());
	format = format.replace(/S/g, time.getMilliseconds());

	format = format.replace(/wwww/g, weekName[time.getDay()]);
	format = format.replace(/www/g, weekName[time.getDay()].substr(0, 3));
	format = format.replace(/WWW/g, '星期' + week[time.getDay()]);
	format = format.replace(/W/g, week[time.getDay()]);

	format = format.replace(/a|A/g, time.getHours() > 12 ? 'PM' : 'AM');
	return format;
}

/*
 * 为兼容采用&nbsp;代替空格后，英文不换行问题。
 * 将所有&nbsp;替换成" &nbsp;"
 */
exports.replaceKongGe = function(str) {
	if(!str) return "";
	var strArr = str.split("&nbsp;");
	var newstr = "";
	for(var i = 0; i < strArr.length; i++) {
		var tempstr = strArr[i];
		if(i == 0) {
			newstr += tempstr;
		} else {
			newstr += " &nbsp;" + tempstr;
		}
	}
	return newstr;
};

/*
 * 获取uuid
 */
exports.uuid = function(len, radix) {
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	var uuid = [],
		i;
	radix = radix || chars.length;

	if(len) {
		// Compact form
		for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		// rfc4122, version 4 form
		var r;

		// rfc4122 requires these characters
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';

		// Fill in random data.  At i==19 set the high bits of clock sequence as
		// per rfc4122, sec. 4.1.5
		for(i = 0; i < 36; i++) {
			if(!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}

	return uuid.join('');
}

module.exports = exports;